package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: lx
 * @Time: 10:38
 * @Description:
 *   购物车管理
 *      查询购物车列表
 *      加入购物车
 *      去购物车结算
 *      + -
 *      删除
 *      清空购物车
 *      选中购物车中的商品
 *
 *      Swagger   测试
 *         /admin/**  后台
 *         /api/**    前台
 */
@RestController
@RequestMapping("/api/cart")
public class CartApiController {


    @Autowired
    private CartInfoService cartInfoService;

    //加入购物车  临时用户 永久用户
    @GetMapping("/addCart/{skuId}/{skuNum}")
    public Result addCart(
            @PathVariable Long skuId,
            @PathVariable Integer skuNum,
            HttpServletRequest request
            ){
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        //加入购物车 并返回最新购物车 页面回显
        cartInfoService.addCart(skuId,skuNum,userId);
        return Result.ok();
    }
    //查询购物车
    @GetMapping("/getCartInfo/{skuId}/{skuNum}")
    public CartInfo getCartInfo(  @PathVariable Long skuId,
                                  @PathVariable Integer skuNum,HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        return cartInfoService.getCartInfo(skuId,skuNum,userId);//10
    }

    //查询购物车列表集合
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request){
        //用户的ID  永久
        String userId = AuthContextHolder.getUserId(request);
        //临时
        String userTempId = AuthContextHolder.getUserTempId(request);
        List<CartInfo> cartInfoList = cartInfoService.cartList(userId,userTempId);
        return Result.ok(cartInfoList);
    }

    //选中或取消
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId, @PathVariable Integer isChecked
      ,HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.checkCart(skuId,isChecked,userId);
        return Result.ok();
    }
    //商品清单
    @GetMapping("/getCartCheckedList")
    public List<CartInfo> getCartCheckedList(HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        return cartInfoService.getCartCheckedList(userId);
    }

}
